cc.Class({
    extends: cc.Component,

    properties: {
        _showtoudaoarray: [],
        _showzhongdaoarray: [],
        _showweidaoarray: [],
        _leftOrright: false,
        _gameNode: null, 
        arrindex: 0,
        
        showtoudaoarray: {
            get: function () {
                return this._showtoudaoarray;
            },
            set: function (value) {
                this._showtoudaoarray = value;
            },
        },
        
        showzhongdaoarray: {
            get: function () {
                return this._showzhongdaoarray;
            },
            set: function (value) {
                this._showzhongdaoarray = value;
            },
        },
        
        showweidaoarray: {
            get: function () {
                return this._showweidaoarray;
            },
            set: function (value) {
                this._showweidaoarray = value;
            },
        },
        
        leftOrright: {
            get: function () {
                return this._leftOrright;
            },
            set: function (value) {
                this._leftOrright = value;
            },
        },
        
        gameNode: {
            get: function () {
                return this._gameNode;
            },
            set: function (value) {
                this._gameNode = value;
            },
        },
    },

    onLoad: function () {
        var res = [
            "combination_node/combinations_right_",
            "combination_node/combinations_left_"
            ];
        for (let i = 0; i < res.length; i++) {
            for (let j = 0; j < 3; j++) {
                var combinations_node = cc.find(res[i] + j, this.node);
                combinations_node.active = false;
            }
        }
        
        var res = [
            "toudao_node",
            "zhongdao_node",
            "weidao_node"
            ];
        for (let i = 0; i < res.length; i++) {
            var dao_node = cc.find(res[i], this.node);
            dao_node.active = false;
        }
        
        //头道翻牌动作
        var toudao_node = cc.find("toudao_node", this.node);
        toudao_node.active = true;
        for (let i = 0; i < 3; i++) {
            var toudaopoker_node = cc.find("toudao_node/poker_" + i, this.node);
            toudaopoker_node.runAction(cc.sequence(
                cc.delayTime(1),
                cc.scaleTo(0.2, 0, 1),
                cc.callFunc(this.onShowToudaoCallback, this)
                ));
        }
    },
    
    //展示头道牌
    onShowToudaoCallback: function (target) {
        target.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("poker_" + this._showtoudaoarray[this.arrindex++].getId());
        target.runAction(cc.sequence(
            cc.scaleTo(0.2, 1, 1),
            cc.delayTime(1),
            cc.callFunc(this.onShowToudaoCombiation, this)
            ));
    },
    
    //显示头道组合
    onShowToudaoCombiation: function () {
        if (this.arrindex !== 3) {
            return;
        }
        var combination_node = cc.find("combination_node/combinations_" + (this._leftOrright? "right": "left") + "_0", this.node);
        var combinations = this.onAnnouncement(this._showtoudaoarray);
        combination_node.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("combinations_" + combinations);
        combination_node.active = true;
        this.arrindex = 0;
        this.node.runAction(cc.sequence(
                cc.delayTime(1),
                cc.callFunc(this.onShowZhongdao, this)
                ));
    },
    
    //中道翻牌动作
    onShowZhongdao: function () {
        var zhongdao_node = cc.find("zhongdao_node", this.node);
        zhongdao_node.active = true;
        for (let i = 0; i < 5; i++) {
            var zhongdaopoker_node = cc.find("zhongdao_node/poker_" + i, this.node);
            zhongdaopoker_node.runAction(cc.sequence(
                cc.delayTime(1),
                cc.scaleTo(0.2, 0, 1),
                cc.callFunc(this.onShowZhongdaoCallback, this)
                ));
        }
    },
    
    //展示中道牌
    onShowZhongdaoCallback: function (target) {
        target.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("poker_" + this._showzhongdaoarray[this.arrindex++].getId());
        target.runAction(cc.sequence(
            cc.scaleTo(0.2, 1, 1),
            cc.delayTime(1),
            cc.callFunc(this.onShowZhongdaoCombiation, this)
            ));
    },
    
    //显示中道组合
    onShowZhongdaoCombiation: function () {
        if (this.arrindex !== 5) {
            return;
        }
        var combination_node = cc.find("combination_node/combinations_" + (this._leftOrright? "right": "left") + "_1", this.node);
        var combinations = this.onAnnouncement(this._showzhongdaoarray);
        combination_node.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("combinations_" + combinations);
        combination_node.active = true;
        
        this.arrindex = 0;
        this.node.runAction(cc.sequence(
                cc.delayTime(1),
                cc.callFunc(this.onShowWeidao, this)
                ));
    },
    
    //尾道翻牌动作
    onShowWeidao: function () {
        var weidao_node = cc.find("weidao_node", this.node);
        weidao_node.active = true;
        for (let i = 0; i < 5; i++) {
            var weidaopoker_node = cc.find("weidao_node/poker_" + i, this.node);
            weidaopoker_node.runAction(cc.sequence(
                cc.delayTime(1),
                cc.scaleTo(0.2, 0, 1),
                cc.callFunc(this.onShowWeidaoCallback, this)
                ));
        }
    },
    
    //展示尾道牌
    onShowWeidaoCallback: function (target) {
        target.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("poker_" + this._showweidaoarray[this.arrindex++].getId());
        target.runAction(cc.sequence(
            cc.scaleTo(0.2, 1, 1),
            cc.delayTime(1),
            cc.callFunc(this.onEndWeidao, this)
        ));
    },
    
    //结束展示牌
    onEndWeidao: function () {
        if (this.arrindex !== 5) {
            return;
        }
        var combination_node = cc.find("combination_node/combinations_" + (this._leftOrright? "right": "left") + "_2", this.node);
        var combinations = this.onAnnouncement(this._showweidaoarray);
        combination_node.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("combinations_" + combinations);
        combination_node.active = true;
        this.node.runAction(cc.sequence(
                cc.delayTime(1),
                cc.callFunc(this.onShowRoundResult, this)
                ));
    },
    
    //显示结算界面
    onShowRoundResult: function () {
        this._gameNode.onShowRoundResult();
    },
    
    //计算牌组的组合
    onAnnouncement: function (array) {
        var combinations = 0;
        var count = 0;
        var duizi = 0;
        var hulu_san = 0;
        var hulu_dui = 0;
        var typeArray = [[],[],[],[]];
        var rankArray = [[],[],[],[],[],[],[],[],[],[],[],[],[]];
        for (let i = 0; i < array.length; i++) {
            switch (array[i].getType()) {
                case 0:typeArray[0].push(i);break;
                case 1:typeArray[1].push(i);break;
                case 2:typeArray[2].push(i);break;
                case 3:typeArray[3].push(i);break;
            }
            switch (array[i].getRank()) {
                case 2:rankArray[0].push(i);break;
                case 3:rankArray[1].push(i);break;
                case 4:rankArray[2].push(i);break;
                case 5:rankArray[3].push(i);break;
                case 6:rankArray[4].push(i);break;
                case 7:rankArray[5].push(i);break;
                case 8:rankArray[6].push(i);break;
                case 9:rankArray[7].push(i);break;
                case 10:rankArray[8].push(i);break;
                case 11:rankArray[9].push(i);break;
                case 12:rankArray[10].push(i);break;
                case 13:rankArray[11].push(i);break;
                case 14:rankArray[12].push(i);break;
            }
        }
        //判断同花或者同花顺
        for (let i = 0; i < typeArray.length; i++) {
            var temp = 0;
            var arr = [];
            if (typeArray[i].length > 4) {
                count = 0;
                for (let i = 0; i < rankArray.length; i++) {
                    if (rankArray[i].length) {
                        count++;
                        if (count >= 5) {
                            combinations = 8;
                            cc.log("是同花顺");
                            return combinations;
                        }
                    } else {
                        count = 0;
                    }
                }
                combinations = 5;
            }
        }
        if (combinations) {
            return combinations;
        }
        
        //判断顺子
        count = 0;
        for (let i = 0; i < rankArray.length; i++) {
            if (rankArray[i].length) {
                count++;
                if (count >= 5) {
                    combinations = 4;
                    cc.log("是顺子");
                    return combinations;
                }
            } else {
                count = 0;
            }
        }   
        
        for (let i = 0; i < rankArray.length; i++) {
            if (rankArray[i].length > 3) {
                combinations = 7;
                cc.log("是铁支");
                return combinations;
            } else if (rankArray[i].length > 2) {
                hulu_san++;
            } else if (rankArray[i].length > 1) {
                hulu_dui++;
                duizi++;
            }
            if (hulu_san) {
                if (hulu_dui) {
                    combinations = 6;
                    cc.log("是葫芦");
                    return combinations;
                } else {
                    combinations = 3;
                    cc.log("是三条");
                    return combinations;
                }
            }
            if (duizi >= 2) {
                combinations = 2;
                cc.log("是两对" + (i + 2));
                return combinations;
            } else if (duizi === 1) {
                combinations = 1;
                cc.log("是一对");
            }
        }
        return combinations;
    },
});
